package service;

import dao.PetDao;
import dao.PetDaoMySQLImpl;
import dao.PetDaoOracleImpl;
import entity.Pet;
import factory.PetDaoFactory;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("petService")
public class PetServiceImpl implements PetService {
    //创建PetDao实现对象
    //注释或删除实例2的简单工厂获取的方式
    //PetDao petDao = PetDaoFactory.getInstance(PetDaoFactory.defaultBeanName);
    //提供petDao属性和对应的getter,setter
    @Resource(type= PetDaoOracleImpl.class)
    private PetDao petDao;

    public PetDao getPetDao(){return petDao;}

    public void setPetDao(PetDao petDao) {
        this.petDao = petDao;
    }

    @Override
    public boolean save(Pet pet) {
        int rows = petDao.insert(pet);

//        if(true){
//            //模拟业务执行过程中出现异常
//            throw new RuntimeException("业务操作异常!");
//        }
        return rows > 0;
    }

    @Override
    public boolean modify(Pet pet) {
        return false;
    }

    @Override
    public boolean del(Integer id) {
        return false;
    }

    //模拟业务执行所需的时间
    private void delay(int time){
        try{
            Thread.sleep(time);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
}
